package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-06-15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    private CategoryVo cateVo;

    @Override
    public List<Category> treeSelect(Long parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, parentId);
        queryWrapper.orderByAsc(Category::getOrderNum);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (Category category : categoryList) {
                //查找当前节点是否有子节点
                Long count = baseMapper.selectCount(
                        new LambdaQueryWrapper<Category>()
                                .eq(Category::getParentId, category.getId())
                );
                category.setHasChildren(count > 0);
            }
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {

        // 查询一级类别列表
        List<Category> categoryList = baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );

        //TODO 将List<Category>转换成List<CategoryVo>
        /*
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(category.getId());
            categoryVo.setName(category.getName());
            categoryVo.setImageUrl(category.getImageUrl());
            categoryVoList.add(categoryVo);
        }*/
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
        return categoryVoList;
    }

    @Override
    public List<CategoryVo> tree() {

        //查询所有分类
        List<Category> allCategoryList = baseMapper.selectList(null);
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
        return buildTree(categoryVoList);
    }

    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        ArrayList<CategoryVo> categoryVoArrayList = new ArrayList<>();
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId().longValue() == 0L) {
                categoryVo = findAndAddChildren(categoryVo, categoryVoList);
                categoryVoArrayList.add(categoryVo);
            }
        }
        return categoryVoArrayList;
    }

    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        for (CategoryVo cateVo : categoryVoList) {
            if (cateVo.getParentId().longValue() == categoryVo.getId().longValue()) {
                cateVo = findAndAddChildren(cateVo, categoryVoList);
                categoryVo.getChildren().add(cateVo);
            }
        }
        return categoryVo;
    }
}
